<?php

function init_manage_menu() {
    if (function_exists("add_submenu_page")) {
        add_dashboard_page("Inbox", "Inbox", "read", "manage-inbox", "message_box_page");
    } else {
        _e("Deprecated Method ! Plugin failed to run !");
        return;
    }
}

function wpmb_screen_icon() {
?>
    <img src="<?php echo path_join(WP_PLUGIN_URL, basename(dirname(__FILE__))) . "/mail-icon.png"; ?>" alt="message" class="wpmb_icon" />
<?php
}

function message_box_page() {
?>
    <div class="wrap">
    <?php wpmb_screen_icon();
    _e("<h2>Welcome to your inbox</h2>"); ?>
    <ul class="subsubsub">
        <li id="inbox_link"><a href="index.php?page=manage-inbox&area=inbox"><?php _e("Inbox"); ?></a><span class="count">(<?php echo wpmb_count_message("inbox"); ?>)</span> |</li>
        <li id="outbox_link"><a href="index.php?page=manage-inbox&area=outbox"><?php _e("Outbox"); ?></a><span class="count">(<?php echo wpmb_count_message("outbox"); ?>)</span> |</li>
        <li id="new_message"><a href="index.php?page=manage-inbox&area=compose"><?php _e("New Message"); ?></a>
    </ul>

    <?php
    if(!empty($_POST) && check_admin_referer("submited","submit-new-message")) {
        $errors = array();
        $title = trim(strip_tags($_POST['title']));
        $to_users = trim(strip_tags($_POST['to_u']));
        $content = trim($_POST['wpmb_message_content']);
        
        $errors = array_merge($errors, data_validation("User", $to_users, "required"));
        $errors = array_merge($errors, data_validation("Title", $title, "required"));
        $errors = array_merge($errors, data_validation("Message", strip_tags($content), "required"));
        if(count($errors) > 0) {
            foreach($errors as $error) {
                $error_result .= $error."<br />";
            }
          
           ?>
             <div class="error" style="margin-top:40px;"><p><?php echo $error_result; ?></p></div>
             <p><a class="button" href="javascript:history.go(-1)">Back</a></p>
     
         <?php
        } else {
            $update_result = wpmb_new_message($to_users, $content,$title);
            var_dump($update_result);
            if($update_result != true) {
 
                ?>
                <div class="warning" style="margin-top:40px;"><p><?php echo "The following users is not found:"."<br />"; ?></p>
                <p><?php foreach($update_result as $result) { echo $result."<br />"; }?></p>
                </div>
                <p><a class="button" href="javascript:history.go(-1)">Back</a></p>
                <?php
               
            }
        }
    }

    if ($_GET['area'] == "inbox") {
        wpmb_table_generator("inbox");
    } elseif ($_GET['area'] == "outbox") {
        wpmb_table_generator("outbox");
    } else if ($_GET['area'] == "compose") {
        wpmb_new_message_full();
    } 

 
    ?>
</div>

<?php
}

function wpmb_new_message_full() {
    if (function_exists("wp_tiny_mce")) {
        wp_tiny_mce(false, array("editor_selector" => "wpmb_message_content"));
    
?>
    <form name="wpmb_newMessage" method="POST" action="index.php?page=manage-inbox&area=submited">
        <div id="wpmb_message_content_wrapper">
            <p>
                <label for="to_u">
                    <strong>Send To:</strong> (Use ";" between username if you want to send to multiple users)<input name="to_u" class="one_line_input" id="to_u"/>
                </label>
            </p>
            <p>
                <label for="title">
                    <strong>Title:</strong><input name="title" class="one_line_input" id="title"/>
                </label>
            </p>

            <label for="wpmb_message_content">
                <strong>Message Content:</strong>
                <div style="border: 1px solid #DDD"><textarea class="wpmb_message_content" id="wpmb_message_content" name="wpmb_message_content"></textarea></div>
            </label>
            <?php echo wp_nonce_field("submited","submit-new-message"); ?>
            <p style="text-align: right;"><input type="submit" value="Send Message" class="button" /></p>
        </div>
    </form>
    <?php } ?>
<?php
} 

add_action("admin_menu", "init_manage_menu");
